﻿using GenSpringCloudProject.projectconfig;
using MyTech.CodeGenerator.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenSpringCloudProject.subproject.config_server
{
    class ConfigServer
    {
        public static bool Gen(ProjectConfig projectConfig, string basePath)
        {
            basePath = basePath + "//configserver";
            FileUtil.SaveCode(basePath + "/pom.xml", GenPom(projectConfig, basePath).ToString());
            string packagePath = projectConfig.groupId.Replace(".", "//");
            FileUtil.SaveCode(basePath + "//src//main//java//" + packagePath + "//configserver//ConfigServerApplication.java", GenConfigServerApplication().ToString());
            FileUtil.SaveCode(basePath + "//src//main//resources//" + "//application.yml", GenApplicationYml().ToString());

            return true;
        }

        public static StringBuilder GenPom(ProjectConfig projectConfig, string basePath)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.AppendLine("<project xmlns=\"http://maven.apache.org/POM/4.0.0\"");
            sb.AppendLine("         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
            sb.AppendLine("         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">");
            sb.AppendLine("    <parent>");
            sb.AppendLine("        <artifactId>"+ projectConfig.projectName + "</artifactId>");
            sb.AppendLine("        <groupId>" + projectConfig.groupId + "</groupId>");
            sb.AppendLine("        <version>" + projectConfig.version + "</version>");
            sb.AppendLine("    </parent>");
            sb.AppendLine("    <modelVersion>4.0.0</modelVersion>");
            sb.AppendLine("");
            sb.AppendLine("    <artifactId>configServer</artifactId>");
            sb.AppendLine("    <groupId>" + projectConfig.groupId + "</groupId>");
            sb.AppendLine("    <version>" + projectConfig.version + "</version>");
            sb.AppendLine("    <packaging>jar</packaging>");
            sb.AppendLine("");
            sb.AppendLine("    <dependencies>");
            sb.AppendLine("        <dependency>");
            sb.AppendLine("            <groupId>org.springframework.cloud</groupId>");
            sb.AppendLine("            <artifactId>spring-cloud-starter-eureka</artifactId>");
            sb.AppendLine("        </dependency>");
            sb.AppendLine("        <dependency>");
            sb.AppendLine("            <groupId>org.springframework.cloud</groupId>");
            sb.AppendLine("            <artifactId>spring-cloud-netflix-eureka-client</artifactId>");
            sb.AppendLine("        </dependency>");
            sb.AppendLine("");
            sb.AppendLine("        <dependency>");
            sb.AppendLine("            <groupId>org.springframework.cloud</groupId>");
            sb.AppendLine("            <artifactId>spring-cloud-config-server</artifactId>");
            sb.AppendLine("        </dependency>");
            sb.AppendLine("");
            sb.AppendLine("        <!-- spring boot test-->");
            sb.AppendLine("        <dependency>");
            sb.AppendLine("            <groupId>org.springframework.boot</groupId>");
            sb.AppendLine("            <artifactId>spring-boot-starter-test</artifactId>");
            sb.AppendLine("            <scope>test</scope>");
            sb.AppendLine("        </dependency>");
            sb.AppendLine("    </dependencies>");
            sb.AppendLine("");
            sb.AppendLine("</project>");



            return sb;
        }


        public static StringBuilder GenConfigServerApplication()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("package com.ydtech.configserver;");
            sb.AppendLine("");
            sb.AppendLine("import org.springframework.boot.SpringApplication;");
            sb.AppendLine("import org.springframework.boot.autoconfigure.SpringBootApplication;");
            sb.AppendLine("import org.springframework.cloud.config.server.EnableConfigServer;");
            sb.AppendLine("import org.springframework.cloud.netflix.eureka.EnableEurekaClient;");
            sb.AppendLine("");
            sb.AppendLine("@SpringBootApplication");
            sb.AppendLine("@EnableConfigServer");
            sb.AppendLine("@EnableEurekaClient");
            sb.AppendLine("public class ConfigServerApplication {");
            sb.AppendLine("");
            sb.AppendLine("    public static void main(String[] args) {");
            sb.AppendLine("        SpringApplication.run(ConfigServerApplication.class, args);");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            sb.AppendLine("");



            return sb;
        }


        public static StringBuilder GenApplicationYml()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("server:");
            sb.AppendLine("  port: 8888");
            sb.AppendLine("spring:");
            sb.AppendLine("  application:");
            sb.AppendLine("    name: configserver");
            sb.AppendLine("  profiles:");
            sb.AppendLine("    active: native");
            sb.AppendLine("  cloud:");
            sb.AppendLine("    config:");
            sb.AppendLine("      server:");
            sb.AppendLine("        native:");
            sb.AppendLine("          searchLocations: d:/conf");
            sb.AppendLine("");
            sb.AppendLine("eureka:");
            sb.AppendLine("  client:");
            sb.AppendLine("    service-url:");
            sb.AppendLine("      defaultZone: http://localhost:8020/eureka/");
            sb.AppendLine("");


            return sb;
        }
    
    }
}
